package user.factory;

import book.Library;
import constant.Constant;
import user.AdminUser;
import user.NormalUser;
import user.User;
import utils.PermissionException;


public class ProxyUser {
    private User realUser;

    public ProxyUser(User user) {
        this.realUser = user;
        library = Library.getLibrary();
    }

    Constant constant = new Constant();

    public User getRealUser() {
        return realUser;
    }

    public Library library;

    public int display() {
        return this.realUser.display();
    }

    //-------------------------------管理员方法-----------------------------------
    //检查当前用户身份是否为管理员
    private void checkRealUserWhetherAdminUser(String message) {
        if (!(this.realUser instanceof AdminUser)) {
            throw new PermissionException(message);
        }
    }

    //上架图书
    public void addBook() {
        System.out.println("代理类的addBook方法执行了");
        try {
            checkRealUserWhetherAdminUser("普通用户没有权限上架图书");
        } catch (PermissionException e) {
            e.printStackTrace();
        }

        ((AdminUser) (this.realUser)).addBook();
    }

    //修改图书
    public void updateBook() {
        System.out.println("代理类的updateBook方法执行了");
        try {
            checkRealUserWhetherAdminUser("普通用户没有权限修改图书");
        } catch (PermissionException e) {
            e.printStackTrace();
        }

        ((AdminUser) (this.realUser)).updateBook();
    }


    //下架图书
    public void removeBook() {
        System.out.println("代理类的removeBook方法执行了");
        try {
            checkRealUserWhetherAdminUser("普通用户没有权限下架图书");
        } catch (PermissionException e) {
            e.printStackTrace();
        }

        ((AdminUser) (this.realUser)).removeBook();

    }

    //查看图书的借阅次数
    public void borrowCount() {
        System.out.println("代理类的borrowCount方法执行了");
        try {
            checkRealUserWhetherAdminUser("普通用户没有权限查看借阅图书的次数");
        } catch (PermissionException e) {
            e.printStackTrace();
        }
        ((AdminUser)(this.realUser)).borrowCount();


    }

    //查看最受欢迎的前N本书
    public void generateBook() {
        System.out.println("代理类的generateBook方法执行了");
        try {
            checkRealUserWhetherAdminUser("普通用户没有权限查看最受欢迎的前N本书");
        } catch (PermissionException e) {
            e.printStackTrace();
        }
        ((AdminUser)(this.realUser)).generateBook();


    }

    //查看库存状态
    public void checkInventoryStatus() {
        System.out.println("代理类的checkInventoryStatus方法执行了");
        try {
            checkRealUserWhetherAdminUser("普通用户没有权限查看图书的库存");
        } catch (PermissionException e) {
            e.printStackTrace();
        }
        ((AdminUser)(this.realUser)).checkInventoryStatus();

    }

    //移除上架超过1年的书籍
    public void checkRemoveOldBooks() {
        System.out.println("代理类的checkRemoveOldBooks方法执行了");

        try {
            checkRealUserWhetherAdminUser("普通用户没有权限移除上架超过一年的图书");
        } catch (PermissionException e) {
            e.printStackTrace();
        }
        ((AdminUser)(this.realUser)).checkRemoveOldBooks();
    }


    //-------------------------------普通用户方法-----------------------------------

    //检查当前用户身份是否为普通用户
    private void checkRealUserWhetherNormalUser(String message) {
        if (!(this.realUser instanceof NormalUser)) {
            throw new PermissionException(message);
        }
    }


    //借阅图书
    public void borrowedBook() {
        System.out.println("代理类的borrowedBook方法执行了");
        checkRealUserWhetherNormalUser("请以普通用户身份借阅图书");
        ((NormalUser)(this.realUser)).borrowedBook();
    }

    //归还图书
    public void returnBook() {
        System.out.println("代理类的returnBook方法执行了");

        checkRealUserWhetherNormalUser("请以普通用户身份归还图书");

        ((NormalUser)(this.realUser)).returnBook();

    }

    //查看当前借阅情况
    public void viewBorrowedHistory() {
        System.out.println("代理类的viewBorrowedHistory方法执行了");
        checkRealUserWhetherNormalUser("请以普通用户身份查看借阅情况");
        ((NormalUser)(this.realUser)).viewBorrowedHistory();


    }

    //-------------------------------------------------
    //选择菜单
    public void handleOperation(int choice) {

        //管理员
        if (this.realUser instanceof AdminUser) {
            switch (choice) {
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.displayBook();
                    break;
                case Constant.EXIT:
                    library.exit();
                    break;
                case Constant.ADD_BOOK:
                    addBook();
                    break;
                case Constant.UPDATE_BOOK:
                    updateBook();
                    break;
                case Constant.REMOVE_BOOK:
                    removeBook();
                    break;
                case Constant.BORROW_COUNT_BOOK:
                    borrowCount();
                    break;
                case Constant.GENERATE_Book:
                    generateBook();
                    break;
                case Constant.CHECK_INVENTORY_STATUS:
                    checkInventoryStatus();
                    break;
                case Constant.CHECK_REMOVE_OLD_BOOK:
                    checkRemoveOldBooks();
                    break;
            }
        } else { //普通用户
            switch (choice) {
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.displayBook();
                    break;
                case Constant.EXIT:
                    library.exit();
                    break;
                case Constant.BORROWED_BOOK:
                    borrowedBook();
                    break;
                case Constant.RETURN_BOOK:
                    returnBook();
                    break;
                case Constant.VIEW_BORROWED_HISTORY:
                    viewBorrowedHistory();
                    break;
            }
        }
    }


}
